package com.mocchira.music.radio;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class FMRTopActivity extends Activity implements OnItemClickListener {

    LinearLayout mCurrentTouch = null;
    LinearLayout mCtlGallery = null;
    Gallery g = null;
    
    public static final int MENU_FAVORITE = 0;
    public static final int MENU_BACK = 1;
    public static final int MENU_HOT = 2;
    public static final int MENU_HOT_TAG = 3;
    public static final int MENU_LATEST_TAG = 4;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        g = (Gallery) findViewById(R.id.gallery);
        g.setOnItemClickListener(this);
        g.setAdapter(new GridAdapter(this));
        mCtlGallery = (LinearLayout)findViewById(R.id.layout_ctl_gallery);

        Button button = new Button(this);
        button.setGravity(Gravity.CENTER);
        button.setText(R.string.btn_artist_channel);
        button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FMRTopActivity.this, HotArtistActivity.class);
                startActivity(intent);
            }
        });
        mCtlGallery.addView(button);
        button = new Button(this);
        button.setGravity(Gravity.CENTER);
        button.setText(R.string.btn_tag_channel);
        button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FMRTopActivity.this, HotTagsActivity.class);
                startActivity(intent);
            }
        });
        mCtlGallery.addView(button);
        button = new Button(this);
        button.setGravity(Gravity.CENTER);
        button.setText(R.string.btn_latest_tag_channel);
        button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FMRTopActivity.this, LatestTracksActivity.class);
                startActivity(intent);
            }
        });
        mCtlGallery.addView(button);
        button = new Button(this);
        button.setGravity(Gravity.CENTER);
        button.setText(R.string.btn_ranking_channel);
        button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FMRTopActivity.this, RankingTabActivity.class);
                startActivity(intent);
            }
        });
        mCtlGallery.addView(button);
    }
    
    protected static final List<List<GalleryItem>> itemLists = new ArrayList<List<GalleryItem>>();
    static {
        List<GalleryItem> itemList = new ArrayList<GalleryItem>();
        itemList.add(new GalleryItem("rock", R.drawable.rock, "Rock"));
        itemList.add(new GalleryItem("indie", R.drawable.indie, "Indie"));
        itemList.add(new GalleryItem("pop", R.drawable.pop, "Pop"));
        itemList.add(new GalleryItem("alternative", R.drawable.alternative, "Alternative"));
        itemList.add(new GalleryItem("electronica", R.drawable.electronic, "Electronica"));
        itemList.add(new GalleryItem("female+vocalists", R.drawable.female, "Female"));
        itemList.add(new GalleryItem("hip-hop", R.drawable.hip_hop, "Hip Hop"));
        itemList.add(new GalleryItem("singer-songwriter", R.drawable.singer_songwriter, "Songwriter"));
        itemList.add(new GalleryItem("indie+rock", R.drawable.indie_rock, "Indie Rock"));
        itemLists.add(itemList);
        itemList = new ArrayList<GalleryItem>();
        itemList.add(new GalleryItem("folk", R.drawable.folk, "Folk"));
        itemList.add(new GalleryItem("rap", R.drawable.rap, "Rap"));
        itemList.add(new GalleryItem("soundtrack", R.drawable.soundtrack, "Soundtrack"));
        itemList.add(new GalleryItem("experimental", R.drawable.experimental, "Experiment"));
        itemList.add(new GalleryItem("punk", R.drawable.punk, "Punk"));
        itemList.add(new GalleryItem("acoustic", R.drawable.acoustic, "Acoustic"));
        itemList.add(new GalleryItem("japanese", R.drawable.japanese, "Japanese"));
        itemList.add(new GalleryItem("soul", R.drawable.soul, "Soul"));
        itemList.add(new GalleryItem("rnb", R.drawable.rnb, "Rnb"));
        itemLists.add(itemList);
        itemList = new ArrayList<GalleryItem>();
        itemList.add(new GalleryItem("indie+pop", R.drawable.indie_pop, "Indie Pop"));
        itemList.add(new GalleryItem("british", R.drawable.british, "British"));
        itemList.add(new GalleryItem("hardcore", R.drawable.hardcore, "Hardcore"));
        itemList.add(new GalleryItem("dance", R.drawable.dance, "Dance"));
        itemList.add(new GalleryItem("jazz", R.drawable.jazz, "Jazz"));
        itemList.add(new GalleryItem("emo", R.drawable.emo, "Emo"));
        itemList.add(new GalleryItem("instrumental", R.drawable.instrumental, "Inst"));
        itemList.add(new GalleryItem("classic+rock", R.drawable.classic_rock, "Classic Rock"));
        itemList.add(new GalleryItem("ambient", R.drawable.ambient, "Ambient"));
        itemLists.add(itemList);
        itemList = new ArrayList<GalleryItem>();
        itemList.add(new GalleryItem("80s", R.drawable.m_80s, "80s"));
        itemList.add(new GalleryItem("punk+rock", R.drawable.punk_rock, "Punk Rock"));
        itemList.add(new GalleryItem("pop+punk", R.drawable.pop_punk, "Pop Punk"));
        itemList.add(new GalleryItem("metal", R.drawable.metal, "Metal"));
        itemList.add(new GalleryItem("canadian", R.drawable.canadian, "Canadian"));
        itemList.add(new GalleryItem("korean", R.drawable.korean, "Korean"));
        itemList.add(new GalleryItem("post-hardcore", R.drawable.post_hardcore, "Post Hardcore"));
        itemList.add(new GalleryItem("funk", R.drawable.funk, "Funk"));
        itemList.add(new GalleryItem("new+wave", R.drawable.new_wave, "New Wave"));
        itemLists.add(itemList);
        itemList = new ArrayList<GalleryItem>();
        itemList.add(new GalleryItem("psychedelic", R.drawable.psychedelic, "Psychedelic"));
        itemList.add(new GalleryItem("classical", R.drawable.classical, "Classical"));
        itemList.add(new GalleryItem("lo-fi", R.drawable.lo_fi, "Lo-Fi"));
        itemList.add(new GalleryItem("j-pop", R.drawable.j_pop, "J-Pop"));
        itemList.add(new GalleryItem("k-pop", R.drawable.k_pop, "K-Pop"));
        itemList.add(new GalleryItem("hard+rock", R.drawable.hard_rock, "Hard Rock"));
        itemList.add(new GalleryItem("screamo", R.drawable.screamo, "Screamo"));
        itemList.add(new GalleryItem("60s", R.drawable.m_60s, "60s"));
        itemList.add(new GalleryItem("piano", R.drawable.piano, "Piano"));
        itemLists.add(itemList);
        itemList = new ArrayList<GalleryItem>();
        itemList.add(new GalleryItem("oldies", R.drawable.oldies, "Oldies"));
        itemList.add(new GalleryItem("french", R.drawable.french, "French"));
        itemList.add(new GalleryItem("country", R.drawable.country, "Country"));
        itemList.add(new GalleryItem("chillout", R.drawable.chillout, "Chillout"));
        itemList.add(new GalleryItem("house", R.drawable.house, "House"));
        itemList.add(new GalleryItem("shoegaze", R.drawable.shoegaze, "Shoegaze"));
        itemList.add(new GalleryItem("90s", R.drawable.m_90s, "90s"));
        itemList.add(new GalleryItem("anime", R.drawable.anime, "Anime"));
        itemList.add(new GalleryItem("reggae", R.drawable.reggae, "Reggae"));
        itemLists.add(itemList);
    }
    
    public class GridAdapter extends BaseAdapter implements OnTouchListener {

        //private Context mContext;
        private int mGalleryItemBackground;
        //private List<View> mViewCache = new ArrayList<View>(FMRTopActivity.itemLists.size());
        private LayoutInflater inflater;

        public GridAdapter(Context c) {
            //mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.FreeMusiRadio);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.FreeMusiRadio_android_galleryItemBackground, 0);
            a.recycle();
            inflater = getLayoutInflater();
            /*
            for (int i = 0; i < FMRTopActivity.itemLists.size(); i++) {
                mViewCache.add(null);
            }
            */
        }

        public int getCount() {
            return FMRTopActivity.itemLists.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        private void inflateTableCol(TableRow row) {
            int i = 0;
            while (i < 3) {
                LinearLayout col = (LinearLayout) inflater.inflate(R.layout.image_col, null);
                col.setOnTouchListener(this);
                col.setBackgroundResource(mGalleryItemBackground);
                row.addView(col);
                i++;
            }
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            TableLayout table;
            if (convertView == null) {
                table = (TableLayout) inflater.inflate(R.layout.image_table, null);
                TableRow row = (TableRow) table.findViewById(R.id.tableRow1);
                inflateTableCol(row);
                row = (TableRow) table.findViewById(R.id.tableRow2);
                inflateTableCol(row);
                row = (TableRow) table.findViewById(R.id.tableRow3);
                inflateTableCol(row);
            } else {
                table = (TableLayout) convertView;
            }
            List<GalleryItem> list = itemLists.get(position);
            int idx = 0;
            for (GalleryItem item: list) {
                int colIdx = idx % 3;
                int rowIdx = idx / 3;
                TableRow row = (TableRow) table.getChildAt(rowIdx);
                LinearLayout col = (LinearLayout) row.getChildAt(colIdx);
                col.setTag(item);
                ImageView imageView = (ImageView)col.findViewById(R.id.genre_image);
                imageView.setImageResource(item.imageId);
                TextView  textView  = (TextView) col.findViewById(R.id.genre_text);
                textView.setGravity(Gravity.CENTER_HORIZONTAL);
                textView.setText(item.displayText);
                idx++;
            }
            /*
            View view = mViewCache.get(position);
            if (view != null) return view;
            TableLayout table = new TableLayout(mContext);
            table.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
            table.setAlwaysDrawnWithCacheEnabled(true);
            table.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES);
            //table.setDrawingCacheEnabled(true);
            TableRow row = null;
            List<GalleryItem> list = itemLists.get(position);
            int idx = 0;
            for (GalleryItem item: list) {
                if ((idx % 3) == 0) {
                    row = new TableRow(mContext);
                }
                LinearLayout col = new LinearLayout(mContext);
                col.setAlwaysDrawnWithCacheEnabled(true);
                col.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES);
                //col.setDrawingCacheEnabled(true);
                col.setOrientation(LinearLayout.VERTICAL);
                col.setOnTouchListener(this);
                ImageView imageView = new ImageView(mContext);
                imageView.setLayoutParams(new LinearLayout.LayoutParams(80, 80));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(3, 3, 3, 3);
                imageView.setImageResource(item.imageId);
                col.addView(imageView);
                TextView textView = new TextView(mContext);
                textView.setTextColor(R.color.display_text);
                textView.setGravity(Gravity.CENTER_HORIZONTAL);
                textView.setText(item.displayText);
                col.addView(textView);
                col.setBackgroundResource(mGalleryItemBackground);
                col.setTag(item);
                row.addView(col);
                if ((idx % 3) == 2) {
                    table.addView(row);
                }
                idx++;
            }
            mViewCache.add(position, table);
            */
            return table;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            LinearLayout target = (LinearLayout)v;
            FMRTopActivity.this.mCurrentTouch = target;
            return false;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Intent intent = new Intent(this, FMRPlayActivity.class);
        Bundle bundle = new Bundle();
        String tag = ((GalleryItem)mCurrentTouch.getTag()).tag;
        bundle.putString(R.string.bundle_tag+"", tag);
        intent.putExtras(bundle);
        startActivity(intent);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_FAVORITE, 0, R.string.favorite);
        menu.add(0, MENU_HOT, 0, R.string.btn_artist_channel);
        menu.add(0, MENU_HOT_TAG, 0, R.string.btn_tag_channel);
        menu.add(0, MENU_LATEST_TAG, 0, R.string.btn_latest_tag_channel);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
        case MENU_FAVORITE:
            Intent intent = new Intent(this, FMRFavoriteActivity.class);
            startActivity(intent);
            break;
        case MENU_HOT:
            intent = new Intent(this, HotArtistActivity.class);
            startActivity(intent);
            break;
        case MENU_HOT_TAG:
            intent = new Intent(this, HotTagsActivity.class);
            startActivity(intent);
            break;
        case MENU_LATEST_TAG:
            intent = new Intent(this, LatestTracksActivity.class);
            startActivity(intent);
            break;
        }
        return super.onMenuItemSelected(featureId, item);
    }
}
